<?php

App::uses('AppModel', 'Model');

/**
 * ModuleSmsAccountInstantMessage Model
 *
 * @property ModuleSmsAccount $ModuleSmsAccount
 */
class ModuleSmsAccountInstantMessage extends AppModel {

    /**
     * Use database config
     *
     * @var string
     */
    public $useDbConfig = 'organisations';

    /**
     * Display field
     *
     * @var string
     */
    public $displayField = 'to';


    //The Associations below have been created with all possible keys, those that are not needed can be removed

    /**
     * belongsTo associations
     *
     * @var array
     */
    public $belongsTo = array(
        'ModuleSmsAccount' => array(
            'className' => 'ModuleSmsAccount',
            'foreignKey' => 'module_sms_account_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Member' => array(
            'className' => 'Member',
            'foreignKey' => 'member_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

    public function getHistory($organisation_id, $limit = 15, $offset = 0) {
        
        $list = $this->find('all', array(
            'contain' => array(
                'ModuleSmsAccount',
                'Member' => array(
                    'MemberImage' => array(
                        'limit' => 1,
                        'order' => array('id' => 'desc')
                    )
                )
            ),
            'conditions' => array(
                'ModuleSmsAccount.organisation_id' => $organisation_id
            ),
            'limit' => $limit,
            'offset' => $offset,
            'order' => array('ModuleSmsAccountInstantMessage.id' => 'desc')
        ));
        
        if( $list ) {
            $data = array();
            foreach($list as $l) {
                $data[] = array(
                    'id' => $l['ModuleSmsAccountInstantMessage']['id'],
                    'first_name' => isset($l['Member']['first_name']) ? $l['Member']['first_name'] : '',
                    'last_name' => isset($l['Member']['last_name']) ? $l['Member']['last_name'] : '',
                    'member_image' => isset($l['Member']['MemberImage'][0]) ? $l['Member']['MemberImage'][0]['url'] : '',
                    'number' => $l['ModuleSmsAccountInstantMessage']['to'],
                    'message' => $l['ModuleSmsAccountInstantMessage']['message'],
                    'sent_at' => $l['ModuleSmsAccountInstantMessage']['sent_at'],
                    'sent' => $l['ModuleSmsAccountInstantMessage']['sent'] 
                );
            }
            
            return array('status' => 'success', 'history' => $data);
        }
        
        return array('status' => 'failed', 'message' => 'No historical records of instant SMS messages found.');
    }
    
    public function logMessage($organisation_id, $member_id, $to, $message, $sent_time, $sent = 1) {
        $account = $this->ModuleSmsAccount->getAccount($organisation_id);
        
        if( $account ) {
            $this->create();
            $this->save(array(
                'module_sms_account_id' => $account['ModuleSmsAccount']['id'],
                'member_id' => $member_id,
                'to' => $to,
                'message' => $message,
                'sent_at' => $sent_time,
                'sent' => $sent
            ));
        }
    }
}
